package com.example.demo.algorithm;

import java.util.*;

public class Start {
    public static void main(String[] args) {
        int[] a = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        for (int n = 1; n <= a.length; n++) {
            int[] b = new int[n];//存放组合结果的数组
            int num = getNumber(a.length, n) / getNumber(n, n);
            System.out.println("当取" + n + "个数时有" + num + "个可能．结果为：");
            submit(a, 0, 0, n, b);
            System.out.println("");
        }
    }

    public static int getNumber(int length, int n) {
        int result;
        if (n == 1) {
            result = length;
        } else {
            result = length * getNumber(length - 1, --n);
        }
        return result;
    }

    public static void submit(int[] a, int c, int i, int n, int[] b) {//a原始数组，c循环的开始值，i数组b索引，n控制递归的次数，b存放结果的数组
        for (int j = c; j < a.length - (n - 1); j++) {
            b[i] = a[j];
            if (n == 1) {
                System.out.println(Arrays.toString(b));
            } else {
                n--;
                i++;
                submit(a, j + 1, i, n, b);//递归调用
                n++;//还原n,i的值e79fa5e98193e78988e69d8331333262356639
                i--;
            }
        }
    }
}
